<?php 
namespace Admin\Controller;

use Think\Controller;
use Think\Upload;
use Think\Image;

class BaseController extends Controller{
    
    public function _initialize(){
        // 判断当前状态是否为登录状态
        if (empty(session('admin'))){
            
            $this->redirect('Login/login');
        }
    }
    
    // 上传图片
    public function uploadImage(){
        $upload = new Upload();
        
        // 设置上传文件的大小
        $upload->maxsize = 2*1024*1024;
        
        // 设置上传文件的类型
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        
        
        // 判断文件目录是否存在，不存在就创建
        if (!file_exists($upload->rootPath)){
            mkdir($upload->rootPath);
        }
        
        // 上传
        $info = $upload->uploadOne($_FILES['imgFile']);
        
        // 获取上传图片路径
        $path = '/Uploads/'.$info['savepath'].$info['savename'];
        $thumb = '/Uploads/'.$info['savepath'].time().'.'.$info['ext'];
        
        if (I('get.theme')==1){
            if (file_exists($_SERVER['DOCUMENT_ROOT'].$path)){
                // 缩略图片
                $image = new Image();
                
                // 打开图片
                $image->open($_SERVER['DOCUMENT_ROOT'].$path);
                
                // 缩略并保存图片
                $image->thumb('122', '86',Image::IMAGE_THUMB_FILLED)->save($_SERVER['DOCUMENT_ROOT'].$thumb);
                
                $image2 = new Image();
                
                // 打开图片
                $image2->open($_SERVER['DOCUMENT_ROOT'].$path);
                $image2->thumb('220', '150',Image::IMAGE_THUMB_FILLED)->save($_SERVER['DOCUMENT_ROOT'].$path);
                
            }
        }
        
        if ($info){ // 上传成功
            
            $this->ajaxReturn(array('error'=>0,'url'=>$path.'|'.$thumb));
        }else {// 上传失败
            
            $this->ajaxReturn(array('error'=>1,'message'=>$upload->getError()));
        }
    }
    
}

?>